const teamModel = require('../models/teams.js')

class TeamCtrl {

    async list() {
        let lst = await teamModel.find().sort('id');
        return lst.map(m=>m.toJSON());
    }

    async create(teamData) {
        let T = new teamModel(teamData);
        const a = await T.save();
        return a.toJSON();
    }

    async retrieve(id) {
        let T = await teamModel.findById(id);
        return T?T.map(e=>e.toJSON()): null;;
    }

    async upsert(id, teamData) {
        teamData.id = id;
        let T = await teamModel.upsert(id, teamData);
        return T.toJSON();
    }
    async removeById(id) {
        await teamModel.removeById(id);
        return 1;
    }
    async find(filter)
    {
        let r = await teamModel.find(filter);
        return r.map(e=>e.toJSON());
    }
}

module.exports = new TeamCtrl();